<template>
  <div style="height: 100%; width: 100%; position: relative">
      <div class="pagetop">
          <div style="width: 100%">
              <div style="float: left; width: 70%">
                  <table border="0">
                      <tr>
                          <td width="80px" align="right">仪器</td>
                          <td>
                              <rm-ddl-instrument @change="labinstrumentchange" width="200px" v-model="instrId" defaultflag="1" prgno="LIS342" :clearable="false" ref="labinstrument"></rm-ddl-instrument>
                          </td>
                          <td>发送日期</td>
                          <td>
                              <el-date-picker v-model="datetime1" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始日期" style="width: 175px" :clearable="false"></el-date-picker>
                          </td>
                          <td width="10px" align="center">至</td>
                          <td>
                              <el-date-picker v-model="datetime2" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束日期" style="width: 175px" :clearable="false"></el-date-picker>
                          </td>
                          <td>
                              <el-radio-group v-model="sendflag">
                                  <table>
                                      <tr>
                                          <td>
                                              <el-radio label="99">全部</el-radio>
                                          </td>
                                          <td>
                                              <el-radio label="0">未发送</el-radio>
                                          </td>
                                          <td>
                                              <el-radio label="1">已发送</el-radio>
                                          </td>
                                          <td>
                                              <el-radio label="9">发送失败</el-radio>
                                          </td>
                                      </tr>
                                  </table>
                              </el-radio-group>
                          </td>
                      </tr>
                      <tr>
                          <td>分单类别</td>
                          <td>
                              <rm-ddl-common @change="reqgroupchange" datatype="reqgroup" dataname="分单类别" v-model="reqgroupval" width="200px"></rm-ddl-common>
                          </td>
                          <td>条码号</td>
                          <td>
                              <el-input v-model="barcode" @keyup.enter.native="scanBarcode(barcode)"></el-input>
                          </td>
                          <td></td>
                          <td style="text-align: left">
                              <el-checkbox true-label="1" false-label="0" v-model="issend">扫描条码立即重新发送</el-checkbox>
                          </td>
                      </tr>
                  </table>
              </div>
              <div style="float: left; width: 30%">
                  <div style="position: absolute; top: 50px; right: 5px">
                      <el-button @click="querysend()" type="primary">查询</el-button>
                      <el-button @click="cleardata()">清除</el-button>
                      <el-button @click="resend()">重新发送</el-button>
                      <el-button @click="instridsendtype()">仪器自动发送类别</el-button>
                  </div>
              </div>
          </div>
      </div>
      <div class="pagecenter">
          <vxe-table :data="tableData" :loading="loading" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row ref="tabledual" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :sort-config="{ trigger: 'cell' }">
              <vxe-table-column type="selection" width="45px" align="center"></vxe-table-column>
              <vxe-table-column type="index" title="No." width="45px" align="center"></vxe-table-column>
              <vxe-table-column field="req_groupna" title="分单类别" width="110px" sortable></vxe-table-column>
              <vxe-table-column field="barcode" title="条码号" width="100px" sortable></vxe-table-column>
              <vxe-table-column field="instrname" title="仪器" width="160px" sortable></vxe-table-column>
              <vxe-table-column field="senddt" title="发送时间" width="125px" align="center" sortable></vxe-table-column>
              <vxe-table-column field="sendflagname" title="发送标志" width="90px" sortable></vxe-table-column>
              <vxe-table-column field="sendmsg" title="发送信息" width="160px" sortable></vxe-table-column>
              <vxe-table-column field="sendfailcnt" title="失败次数" width="70px" sortable></vxe-table-column>
          </vxe-table>
      </div>
      <el-dialog width="600px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="仪器自动发送类别" :visible.sync="dialogTableVisible">
          <template>
              <div style="width: 100%; position: relative">
                  <div style="height: 40px; width: 100%">
                      <table style="font-size: 12px; white-space: nowrap; max-width: 600px">
                          <tr>
                              <td style="width: 600px; text-align: right">
                                  <el-button @click="btnAdd()" icon="el-icon-add">新增</el-button>
                                  <el-button @click="btnDelete()" icon="el-icon-delete">删除</el-button>
                                  <el-button @click="btnSave()" icon="el-icon-save">保存</el-button>
                                  <el-button @click="btnReturn()">返回</el-button>
                              </td>
                          </tr>
                      </table>
                  </div>
                  <div style="height: 400px; top: 60px; width: 100%">
                      <vxe-table
                          :data.sync="dialogtableData"
                          :loading="dialogloading"
                          ref="dialogTable"
                          :select-config="{ reserve: true }"
                          :sort-config="{ trigger: 'cell' }"
                          @cell-dblclick="opendialog"
                          border
                          fit
                          height="auto"
                          header-align="center"
                          highlight-current-row
                          highlight-hover-row
                          class="mytablestyle"
                          highlight-hover-column
                          resizable
                          show-header-overflow="tooltip"
                          show-overflow="tooltip"
                          size="mini"
                          stripe
                          :edit-config="{
                              trigger: 'click',
                              mode: 'cell',
                              showStatus: true,
                              showIcon: false,
                              activeMethod: cellactiveMethod,
                          }"
                          @current-change="currentChangeEvent"
                      >
                          <vxe-table-column field="req_groupname" title="分单类别(双击开窗)" sortable width="130"></vxe-table-column>
                          <vxe-table-column field="pat_typename" title="病人类型(双击开窗)" sortable width="130"></vxe-table-column>
                          <vxe-table-column field="performbyname" title="执行科室(双击开窗)" sortable width="130"></vxe-table-column>
                          <vxe-table-column field="seq" title="序号" sortable width="60" header-align="center" align="right"></vxe-table-column>
                          <vxe-table-column field="instrid" title="仪器id" sortable width="100" header-align="center" align="right"></vxe-table-column>
                      </vxe-table>
                  </div>
              </div> </template
      ></el-dialog>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="请选择科室" :visible.sync="deptdialog">
          <rm-pop-common @change="deptchange" @cancel="deptdialog = false" datatype="dept" irrelevant="deptShowIrrelevant"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="病人类别选择" :visible.sync="pattypedialog">
          <rm-pop-common datatype="seccode" kindno="PT" dataname="病人类别选择" @change="pattypechange" @cancel="pattypedialog = false" irrelevant="pattypeShowIrrelevant"></rm-pop-common>
      </el-dialog>

      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="分单类别" :visible.sync="reqgroupnodialog">
          <rm-pop-common datatype="reqgroup" dataname="分单类别" @change="reqgroupchange" @cancel="reqgroupnodialog = false" irrelevant="reqgroupnoShowIrrelevant"></rm-pop-common>
      </el-dialog>
  </div>
</template>


<script>
import XEUtils from "xe-utils"; //引入工具类
export default {
  name: "LIS342",
  data() {
      return {
          instrId: "",
          datetime1: XEUtils.toDateString(new Date(), "yyyy-MM-dd") + " 00:00:00",
          datetime2: XEUtils.toDateString(new Date(), "yyyy-MM-dd") + " 23:59:59",
          reqgroupval: "",
          barcode: "",
          sendflag: "99",
          tableData: [],
          loading: false,
          issend: "0",
          dialogTableVisible: false,
          dialogloading: false,
          dialogtableData: [],
          actionindex: 0,
          deptdialog: false,
          pattypedialog: false,
          reqgroupnodialog: false,
          tempindex: 0,
      };
  },
  created() {},
  mounted() {},
  updated() {},
  methods: {
      querysend() {
          this.loading = true;
          this.$tool
              .request({
                  url: "/LIS/LIS3/LIS342",
                  method: "post",
                  data: {
                      action: "querysend",
                      datetime1: this.datetime1,
                      datetime2: this.datetime2,
                      instrId: this.instrId, // 仪器
                      barcode: this.barcode, // 条码号
                      sendflag: this.sendflag, // 发送标志
                      reqgroupval: this.reqgroupval, // 分单类别
                  },
              })
              .then((res) => {
                  this.tableData = res.data;
                  this.loading = false;
              })
              .catch((err) => {
                  this.loading = false;
              });
      },
      resend() {
          var data = this.$refs.tabledual.getSelectRecords();
          if (data.length > 0) {
              let barcodes = "";
              data.forEach((item) => {
                  barcodes += item.barcode + ",";
              });
              barcodes = barcodes.substring(0, barcodes.length - 1);
              let msg = "";
              if (data.length == 1) {
                  msg = "您确定要重新发送条码号为【" + barcodes + "】的双工申请吗?";
              } else {
                  msg = "您确定要重新发送双工申请吗，共【" + data.length + "】份?";
              }
              this.$confirm(msg, "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  focusCancelButton: true,
                  closeOnClickModal: false,
                  type: "warning",
              }).then(() => {
                  this.$tool
                      .request({
                          url: "/LIS/LIS3/LIS342",
                          method: "post",
                          data: {
                              action: "resend",
                              barcodes: barcodes,
                              instrId: this.instrId,
                          },
                      })
                      .then((res) => {
                          if (res.flag == "1") {
                              this.$message({
                                  message: res.msg,
                                  type: "success",
                              });
                              this.querysend();
                          } else {
                              if (res.hasOwnProperty("msg")) {
                                  this.$message.error("重新发送错误:" + res.msg);
                              } else {
                                  this.$message.error("重新发送错误:" + res.errmsg);
                              }
                          }
                      })
                      .catch((err) => {
                          this.$message.error("重新发送错误:" + err);
                      });
              });
          } else {
              this.$confirm("请选择要重新发送的数据!", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  focusCancelButton: false,
                  closeOnClickModal: false,
                  type: "warning",
              });
          }
      },
      cleardata() {
          this.datetime1 = XEUtils.toDateString(new Date(), "yyyy-MM-dd") + " 00:00:00";
          this.datetime2 = XEUtils.toDateString(new Date(), "yyyy-MM-dd") + " 23:59:59";
          this.reqgroupval = "";
          this.barcode = "";
          this.sendflag = "99";
          this.tableData = [];
          this.issend = "0";
      },
      labinstrumentchange(id, name) {
          this.instrId = id;
          this.$nextTick(() => {
              this.querysend();
          });
      },
      reqgroupchange(id, name) {
          this.reqgroupval = id;
          this.$nextTick(() => {
              this.querysend();
          });
      },
      scanBarcode(barcode) {
          if (this.issend == "1") {
              this.$tool
                  .request({
                      url: "/LIS/LIS3/LIS342",
                      method: "post",
                      data: {
                          action: "barcodesend",
                          instrId: this.instrId, // 仪器
                          barcode: barcode, // 条码号
                      },
                  })
                  .then((res) => {
                      this.barcode = this.barcode + "  " + res.msg;
                      if (res.flag == "1") {
                          this.querysend();
                      }
                  })
                  .catch((err) => {});
          } else {
              this.querysend();
          }
      },
      instridsendtype() {
          this.dialogTableVisible = true;
          this.$tool
              .request({
                  url: "/LIS/LIS3/LIS342",
                  method: "post",
                  data: {
                      action: "queryautosend",
                      instrId: this.instrId,
                  },
              })
              .then((res) => {
                  this.dialogtableData = res.data;
              })
              .catch((err) => {});
      },
      btnAdd() {
          let dtabledate = this.$refs.dialogTable.tableData;
          let len = (dtabledate.length + 1) * 10;
          this.$refs.dialogTable
              .insert({
                  req_groupid: "",
                  req_groupname: "",
                  pat_typecode: "",
                  pat_typename: "",
                  performby: "",
                  performbyname: "",
                  seq: len,
                  instrId: "",
              })
              .then(({ row }) => {
                  this.$nextTick(() => {
                      this.curselectrow = row;
                      this.$refs.dialogTable.setCurrentRow(row);
                      // this.$refs.dialogTable.setActiveRow(row, 'rpt_itemname');
                  });
              });
          this.actionindex = 1; //新增
      },
      btnDelete() {
          this.actionindex = 3;
          //删除
          try {
              var delrow = this.$refs.dialogTable.getCurrentRow();
              if (delrow) {
                  this.$confirm("确定要删除这条数据吗？", "提示", {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                  })
                      .then(() => {
                          var alld = this.$refs.dialogTable.tableData;
                          var rowindex = alld.indexOf(delrow);
                          this.$refs.dialogTable.remove(delrow);
                          let havedata = this.$refs.dialogTable.tableData;
                          if (havedata.length > 0) {
                              if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                              let selectrow = havedata[rowindex];
                              this.$refs.dialogTable.setCurrentRow(selectrow);
                          }
                      })
                      .catch(() => {});
              }
          } catch (e) {
              console.log("error", e);
          }
      },
      btnSave() {
          let dtabledate = this.$refs.dialogTable.tableData;
          dtabledate.sort((a, b) => {
              return a.seq - b.seq;
          });
          for (let i = 0, len = dtabledate.length; i < len; i++) {
              dtabledate[i].seq = (i + 1) * 10;
          }
          let data = this.$refs.dialogTable.getRecordset();
          let filterdata = dtabledate.filter((item) => item.req_groupid == "" || item.pat_typecode == "" || item.performby == "");
          if (filterdata.length > 0) {
              this.$message.warning("保存的分单类别、执行科室和病人类型不能为空，请开窗选择！");
              return false;
          }
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS342",
                  method: "post",
                  data: {
                      action: "saveautosend",
                      datalist: data,
                      instrId: this.instrId,
                  },
              })
              .then((response) => {
                  if (response.errcode == "1") {
                      this.$message({
                          message: "操作成功！",
                          type: "success",
                      });
                      this.instridsendtype();
                  } else {
                      this.$message.warning("保存失败！" + response.errmsg);
                  }
              });
      },
      btnReturn() {
          this.dialogTableVisible = false;
      },
      opendialog({ row, column }) {
          if (column.property == "req_groupname") {
              this.reqgroupnodialog = true;
          } else if (column.property == "pat_typename") {
              this.pattypedialog = true;
          } else if (column.property == "performbyname") {
              this.deptdialog = true;
          } else {
          }
      },
      cellactiveMethod({ row, column, cellValue }) {
          let columnArr = ["req_groupid", "pat_typecode", "performby", "seq", "instrid"];
          if (columnArr.includes(column.property)) {
              return false;
          } else {
              return true;
          }
      },
      deptchange(code, name) {
          this.curselectrow.performby = code;
          this.curselectrow.performbyname = name;
          this.deptdialog = false;
      },
      pattypechange(code, name) {
          this.curselectrow.pat_typecode = code;
          this.curselectrow.pat_typename = name;
          this.pattypedialog = false;
      },
      reqgroupchange(code, name) {
          this.curselectrow.req_groupid = code;
          this.curselectrow.req_groupname = name;
          this.reqgroupnodialog = false;
      },
      currentChangeEvent({ row, $rowIndex }) {
          this.tempindex = $rowIndex;
          this.actionindex = 2; //修改
          this.curselectrow = row;
      },
  },
};
</script>

<style scoped>
/deep/.el-input.is-disabled .el-input__inner {
  color: black;
}
/deep/.el-checkbox.is-disabled .el-checkbox__label {
  color: black;
}
/deep/.el-textarea.is-disabled .el-textarea__inner {
  color: black;
}
/deep/.vxe-button--wrapper {
  width: 100%;
}
.pagetop {
  left: 0;
  right: 0;
  height: 80px;
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 85px;
  width: 100%;
  left: 0;
  bottom: 0px;
}

.middle-center {
  position: absolute;
  height: 100%;
  left: 505px;
  right: 0;
}

.middle-left {
  position: absolute;
  height: 100%;
  width: 500px;
  left: 0px;
  top: 0;
}

table tr td:nth-child(odd) {
  text-align: right;
  line-height: 32px;
  white-space: nowrap;
}
table tr td:nth-child(even) {
  text-align: left;
}
table {
  font-size: 12px;
}
td {
  padding: 0 5px 0 0;
}
/deep/.el-checkbox__inner::after {
  border: 2px solid #000000 !important;
  box-sizing: content-box;
  content: "";
  border-left: 0 !important;
  border-top: 0 !important;
  height: 7px;
  left: 4px;
  position: absolute;
  top: 1px;
  transform: rotate(45deg) scaleY(0);
  width: 3px;
  transition: transform 0.15s ease-in 0.05s;
  transform-origin: center;
}
/deep/.el-checkbox__input.is-disabled + span.el-checkbox__label {
  color: #000000;
  cursor: not-allowed;
}
/deep/ .el-checkbox__input.is-disabled .el-checkbox__inner,
.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner + .el-input.is-disabled .el-input__inner {
  background-color: #ffffff !important;
}
</style>